package com.example.viewLogic
{
	import com.example.model.MemberShipModel;
	import com.example.view.MemberShipListCanvas;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.IMXMLObject;
	import mx.events.DataGridEvent;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;

	public class MemberShipListCanvasLogic implements IMXMLObject
	{
		private var _view:MemberShipListCanvas;
		private var _memverShipList:ArrayCollection;
		
		public function MemberShipListCanvasLogic()
		{
		}
		
		public function initialized(document:Object, id:String):void
		{
			_view = MemberShipListCanvas(document);
			_view.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationCompleteHandler);
		}
		
		private function onCreationCompleteHandler(event:FlexEvent):void
		{			
			eventHandler();
			viewInitialize();
		}
		
		private function viewInitialize():void
		{
			
			// DataGridの初期値設定
			_memverShipList= new ArrayCollection();
			
			var model:MemberShipModel = new MemberShipModel();
			model.employNo = "0001";
			model.name = "yamada taro";
			model.mobilePhone = "08011111111";
			model.eMailForMobile = "yamada@mobile.com";
			_memverShipList.addItem(model);
			
			model = new MemberShipModel();
			model.employNo = "0002";
			model.name = "koiso takaki";
			model.mobilePhone = "08022222222";
			model.eMailForMobile = "t_koiso@mobile.com";
			_memverShipList.addItem(model);
			
			model = new MemberShipModel();
			model.employNo = "0003";
			model.name = "takaki";
			model.mobilePhone = "08033333333";
			model.eMailForMobile = "takaki@mobile.com";
			_memverShipList.addItem(model);
			
			model = new MemberShipModel();
			model.employNo = "0004";
			model.name = "test test";
			model.mobilePhone = "09099999999";
			model.eMailForMobile = "test@mobile.com";
			_memverShipList.addItem(model);

			_view.dataGrid.dataProvider = _memverShipList;
			
			_memverShipList.disableAutoUpdate();
	
			
			// ComboBoxの初期値設定
			var positionList:ArrayCollection = new ArrayCollection(
				[{label:""},
				{label:"General Manager"},
				{label:"Manager"},
				{label:"Chief"},
				{label:"Staff"}]);
			
			_view.comboBoxPosition.dataProvider = positionList;
			
			
			// Buttonは初期表示時は非表示
			_view.buttonUpdate.visible = false;
		}
		
		private function eventHandler():void
		{
			
			var dataGrid:DataGrid = _view.dataGrid;
			dataGrid.addEventListener(ListEvent.CHANGE, dataGridChangeHandler);
			
			var buttunUpdate:Button = _view.buttonUpdate;
			buttunUpdate.addEventListener(MouseEvent.CLICK, buttunUpdateClickHandler);
		}
		
				
		protected function dataGridChangeHandler(event:ListEvent):void
		{
			
			// 選択された行の情報を入力エリアに反映する
			var dataGrid:DataGrid = _view.dataGrid;
			var model:MemberShipModel = MemberShipModel(dataGrid.selectedItem);
							 	 
			_view.lavelEmpNo.text = model.employNo;
			_view.txtName.text = model.name;
			_view.txtPhone.text = model.mobilePhone;
			_view.txtMailMobile.text = model.eMailForMobile;
			
			_view.comboBoxPosition.selectedItem = getOfficialPosition(model.officialPositionIndex);
			_view.txtEmail.text = model.eMail;
			 
			
			// Buttonを表示
			_view.buttonUpdate.visible = true;
		}
		
		private function getOfficialPosition (index:int):Object
		{
			// employNoに紐づくOfficialPositionの情報を取得する
			var position:ArrayCollection = ArrayCollection(_view.comboBoxPosition.dataProvider);

			return position[index];
		}
		
		protected function buttunUpdateClickHandler(event:MouseEvent):void
		{
			var dataGrid:DataGrid = _view.dataGrid;
						
			// DataGridの更新		
			var model:MemberShipModel = MemberShipModel(dataGrid.selectedItem);
	
			model.name = _view.txtName.text;
			model.mobilePhone = _view.txtPhone.text;
			model.eMailForMobile = _view.txtMailMobile.text;			
			model.officialPositionIndex = _view.comboBoxPosition.selectedIndex;
			model.eMail = _view.txtEmail.text;
			_memverShipList.setItemAt(model, dataGrid.selectedIndex);
			
			var col:Array = dataGrid.columns;
			dataGrid.columns = col;
		}
	}
}